<template>
	<view class="app">
		<view class="center-text">
			<view v-if="shoppingCartList.length > 0">
				<uni-table ref="table" border stripe type="selection" emptyText="暂无更多数据"
					@selection-change="selectionChange">
					<uni-tr>
						<uni-th align="center" style="width: 280px;">商品</uni-th>
						<uni-th align="center">设置</uni-th>
					</uni-tr>
					<uni-tr v-for="item in shoppingCartList" :key="item.id">
						<uni-td>
							<view>
								<image :src="item.productImg" mode="aspectFill"
									style="width: 120rpx; height: 120rpx; border-radius: 10%; float: left;"></image>
							</view>
							<view>
								<view style="text-align: center;">
									{{item.productName}}
								</view>
								<view style="text-align: center;">￥{{item.buyPrice}}</view>
								&nbsp;
								<view style="text-align: center;">
									数量:{{item.buyNum}}
								</view>
							</view>
						</uni-td>
						<uni-td>
							<view class="uni-group">
								<uni-number-box @click.native='updatePrice' :min="1" :max="10" v-model="item.buyNum" />
							</view>
						</uni-td>
					</uni-tr>
				</uni-table>
				<view class="placeOrderWrapper" style="z-index: 99;">
					<!-- 下单按钮 -->
					<view class="placeOrder">
						<view style="font-size: 40rpx; float: left; margin-left: 200rpx; line-height: 60px;">
							合计:￥{{shoppingCart.totalPrice}}</view>
						<uni-icons class="btn" @click="submitOrder" type="wallet-filled" size="25"
							style="color: aliceblue; border-radius: 40rpx; float: right; margin-right: 14px; line-height: 60px; ">去结算</uni-icons>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import lxcCount from '@/components/lxc-count/lxc-count.vue'

	export default {
		components: {
			lxcCount
		},
		data() {
			return {
				index: 0,
				shoppingCartList: [],
				shoppingCart: {
					totalPrice: 0
				},
				productDTO: [],
				quantity: 1,
				selectedCount: 0,
			}
		},
		mounted() {
			window.vue = this;
			this.updateTotalPrice();
		},
		methods: {
			selectionChange(e) {
				console.log(e.detail);
				let selectedIndexs = [] // 选择的下标数组
				selectedIndexs = e.detail.index;
				this.productDTO = [];
				// 将数组中的值，作为table表格数组的下标来获取对应的当前行数据
				selectedIndexs.forEach(e => {
					let test = {};
					test.id = this.shoppingCartList[e].productId;
					test.shoppingCartId = this.shoppingCartList[e].productId;
					test.buyNum = this.shoppingCartList[e].buyNum;
					test.sign = "sc"
					this.productDTO.push(test)
				})
			},
			updatePrice() {
				this.shoppingCart.totalPrice = 0;
				this.shoppingCartList.forEach(e => {
					this.shoppingCart.totalPrice += e.buyNum * e.buyPrice;
				})
			},
			selectShoppingCart() {
				uni.request({
					url: 'api/product-server/selectShoppingCartList',
					method: "GET",
					header: {
						"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ6eU1hbmFnZSI6eyJpZCI6IjE4MjA3NzkzMTgxODg1NzY3NjgiLCJtYW5hZ2VBY2NvdW50IjoiY2oiLCJtYW5hZ2VOaWNrTmFtZSI6ImNqIn19.AI1W3jxpA1T1k438YI8weWTw_51jKU2uUQAjKKJ3Rig"
					},
					success: (res) => {
						if (res.data.code == 200) {
							console.log(res.data)
							this.shoppingCartList = res.data.data || [];
							this.shoppingCartList.forEach(e => {
								this.shoppingCart.totalPrice = this.shoppingCart.totalPrice + e
									.totalPrice;
							})
						}
					}
				});
			},
			submitOrder() {
				uni.request({
					url: 'api/rabbit-server/addOrder',
					method: "POST",
					data: this.productDTO,
					header: {
						"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ6eU1hbmFnZSI6eyJpZCI6IjE4MjA3NzkzMTgxODg1NzY3NjgiLCJtYW5hZ2VBY2NvdW50IjoiY2oiLCJtYW5hZ2VOaWNrTmFtZSI6ImNqIn19.AI1W3jxpA1T1k438YI8weWTw_51jKU2uUQAjKKJ3Rig"
					},
					success: (res) => {
						console.log(res.data);
						// 显示加载遮罩
						uni.showLoading({
							title: '加载中...',
							mask: true // 显示遮罩层，防止用户触摸屏幕
						});

						// 延迟3秒后隐藏加载遮罩
						setTimeout(function() {
							uni.hideLoading();
							uni.showToast({
								title: '下单成功',
								icon: 'success',
								duration: 2000
							});
							if (res.data.code == 200) {
								uni.navigateTo({
									url: "/pages/payOrder/payOrder?id=" + res.data.data,
								})
							} else {
								uni.showToast({
									title: '系统繁忙，请稍后再试',
									icon: 'error',
									duration: 2000
								});
							}
						}, 3000);
					}
				})
			}
		},
		onLoad(e) {
			this.selectShoppingCart();
		},
		mounted() {
			// window.vue = this;
		}
	}
</script>

<style scoped>
	.bar {
		position: fixed;
		z-index: 1000;
		/* 确保 bar 在其他元素之上 */
		display: flex;
		width: 100%;
	}

	.app {
		position: relative;
	}

	.couponWrapper {
		width: 100%;
		height: 60rpx;
		margin-top: 20rpx;
		// border: 1px solid red;

/* 		.tag-view {
			margin-left: 10rpx;
		} */
	}

	.placeOrderWrapper {
		width: 750rpx;
		height: 60px;
		background-color: #E4080A;
		position: fixed;
		bottom: 100rpx;
	}
</style>